import 'package:common_utils/common_utils.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';
import 'package:xinxuan/common/dialogs/del_dialog.dart';
import 'package:xinxuan/controllers/common_page_controller.dart';
import 'package:xinxuan/controllers/seckill_controller/seckill_order_page_controller.dart';
import 'package:xinxuan/http/http_util_service.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:xinxuan/vo/blindbox/blindbox_order_detail_vo.dart';
import 'package:xinxuan/vo/common_vo/common_address_item_vo.dart';

class SeckillOrderDetailPageController extends GetxController {

  final CommonPageController commonPageController = Get.find();
  SeckillOrderPageController? seckillOrderPageController;

  /// 订单详情数据
  var seckillOrderDetailData = BlindBoxOrderDetailData(id: 0, totalAmount: '0.00', status: 0, isCheck: 0).obs;
  /// 标题
  var title = ''.obs;
  /// 同意协议
  var consentAgreement = true.obs;
  /// 倒计时剩余时间
  var duration = 0.obs;
  /// 时间倒计时
  TimerUtil? _timerUtil; 
  /// 选中的地址
  var addressItem = CommonAddressItem(id: 0).obs;
  /// 是否由订单列表页面进入
  bool isOrderPage = true;

  @override
  void onReady() {
    Map<String, dynamic> argumentsMap = Get.arguments;
    int orderId = argumentsMap['seckillOrderId'];
    title.value = argumentsMap['title'] ?? '订单详情';
    isOrderPage = argumentsMap['isOrderPage'] ?? true;
    getSeckillOrderDetail(orderId);
    if (isOrderPage) {
      seckillOrderPageController = Get.find<SeckillOrderPageController>();
    }
    super.onReady();
  }
  
  /// 获取订单详情
  getSeckillOrderDetail(int orderId, {bool loading = true}) async {
    BlindBoxOrderDetailVo seckillOrderDetailVo = await HttpUtilService.getSeckillOrderDetail(orderId: orderId, loading: loading);
    seckillOrderDetailData.value = seckillOrderDetailVo.data!;
    // seckillOrderDetailData.value.status = 5;
    // seckillOrderDetailData.value.isCheck = 1;
    addressItem.value = CommonAddressItem(
      id: seckillOrderDetailData.value.address!.addressId,
      consignee: seckillOrderDetailData.value.address!.consignee,
      mobile: seckillOrderDetailData.value.address!.mobile,
      province: seckillOrderDetailData.value.address!.province,
      city: seckillOrderDetailData.value.address!.city,
      district: seckillOrderDetailData.value.address!.district,
      provinceStr: seckillOrderDetailData.value.address!.provinceStr,
      cityStr: seckillOrderDetailData.value.address!.cityStr,
      districtStr: seckillOrderDetailData.value.address!.districtStr,
      address: seckillOrderDetailData.value.address!.address,
    );
    title.value = seckillOrderDetailData.value.statusStr ?? '';
    // _calculationTime(orderId);
    update();
  }

  /// 计算时间
  // _calculationTime(int orderId) {
  //   if (_timerUtil != null) {
  //     _timerUtil?.cancel();
  //   }
  //   if (seckillOrderDetailData.value.status != 1) {
  //     return;
  //   }
  //   _timerUtil = TimerUtil(mInterval: 1000, mTotalTime: seckillOrderDetailData.value.remainingTime! * 1000);
  //   _timerUtil!.setOnTimerTickCallback((int tick) {
  //     duration.value = (tick ~/ 1000);
  //     if (tick.toInt() <= 0) {
  //       _timerUtil!.cancel();
  //       getSeckillOrderDetail(orderId);
  //     }
  //   });
  //   _timerUtil!.startCountDown();
  // }

  /// 去支付
  toPay(String payableAmount, String orderSn, int id, int status) async {
    Get.toNamed(
      '${AppRouters.COMMON_PAGE}${AppRouters.SECKILL_ORDER_PAY_PAGE}',
      arguments: {
        'payAmount': payableAmount,
        'orderSn': orderSn
      }
    )!.then((res) {
      getSeckillOrderDetail(seckillOrderDetailData.value.id!);
      if (isOrderPage) {
        seckillOrderPageController!.getOrderDetail(status, id);
      }
    });
  }

  /// 取消订单
  closeSeckillOrder() async {
    Get.dialog(DelDialog(message: '确认取消订单吗？', onTap: () async {
      var res = await HttpUtilService.closeSeckillOrder(id: seckillOrderDetailData.value.id!);
      if (res) {
        getSeckillOrderDetail(seckillOrderDetailData.value.id!);
        EasyLoading.showToast('取消成功');
        commonPageController.getUserInfo(loading: false);
        if (isOrderPage) {
          seckillOrderPageController!.updateOrdersStatusClose(seckillOrderDetailData.value.id!);
        }
        update();
      }
    }));
  }

  /// 确认收货
  confirmReceipt() async {
    if (seckillOrderDetailData.value.id! <= 0) {
      EasyLoading.showToast('数据加载中');
      return;
    }
    var res = await HttpUtilService.confirmReceipt(orderId: seckillOrderDetailData.value.id!);
    if (res) {
      EasyLoading.showToast('收货成功');
      getSeckillOrderDetail(seckillOrderDetailData.value.id!, loading: false);
      if (isOrderPage) {
        seckillOrderPageController!.getOrderDetail(seckillOrderDetailData.value.status!, seckillOrderDetailData.value.id!, loading: false);
      }
    }
  }

  /// 修改订单地址
  changeOrderAddress() async {
    bool res = await HttpUtilService.changeOrderAddress(addressId: addressItem.value.id!, orderId: seckillOrderDetailData.value.id!);
    if (res) {
      EasyLoading.showToast('修改地址成功');
    }
  }
  

  @override
  void onClose() {
    _timerUtil?.cancel();
    super.onClose();
  }
  

}